package aod.android.com.bernay.fixing.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.uuzuche.lib_zxing.activity.CaptureActivity;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import aod.android.com.bernay.R;
import aod.android.com.bernay.base.BernayBaseActivity;
import chen.lion.hilib.view.bind.HiView;

public class FixingAddActivity extends BernayBaseActivity {


    public static final int REQUEST_CODE = 1087;

    @HiView(R.id.fixing_ed_name)
    public EditText mCodeEdit;
    private String mCodeEditStr;
    @HiView(R.id.code_ed)
    private EditText code_ed;
    private String code_edStr;
    @Override
    protected int initLayout() {
        return R.layout.activity_fixing_add;
    }

    @Override
    protected void initData() {
        //添加设备kljlkjlkjljkljljljkljlkjl
        initTitle(getString(R.string.add_fixing_name), getString(R.string.add_fixing_next), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCodeEditStr = mCodeEdit.getText().toString();
                code_edStr = code_ed.getText().toString();

                if(mCodeEditStr.equals("")){
                    showError("请输入设备信息");
                    return;
                }else if(code_edStr.equals("")){
                    showError("请输入设备验证码");
                    return;
                }
                Bundle bundle = new Bundle();
                bundle.putString("fixingname", "0");
                bundle.putString("SnNumber", mCodeEditStr);
                bundle.putString("CheckCode", code_edStr);
                goActivity(FixingAddNameActivity.class, bundle);
                finish();
            }
        });

        findViewById(R.id.fixing_iv_scan).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, CaptureActivity.class);
                startActivityForResult(intent, REQUEST_CODE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE) {
            //处理扫描结果（在界面上显示）
            if (null != data) {
                Bundle bundle = data.getExtras();
                if (bundle == null) {
                    return;
                }
                if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
                    String result = bundle.getString(CodeUtils.RESULT_STRING);
                    mCodeEdit.setText(result.trim());
                    //   Toast.makeText(this, "解析结果:" + result, Toast.LENGTH_LONG).show();
                } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {

                }
            }
        }
    }
}
